import java.util.Scanner;

public class Calendar {
	public static void main(String[] args) {
		int year = 0, month = 0;
		System.out.println("请输入你要查询的年份(大于1900)：");
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		year = input.nextInt();
		if (year >= 1900 && year < 2500) {

		} else {
			System.out.println("输入有错！");
			System.exit(0);
		}

		System.out.println("请输入你要查询的月份：");
		month = input.nextInt();
		if (month > 0 && month < 13) {

		} else {
			System.out.println("输入有错！");
			System.exit(0);
		}
		printCalender(year, month);
	}

	public static boolean isLeap(int year) {
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
			return true;
		} else {
			return false;
		}
	}

	// 判断某年某月有多少天
	public static int days(int year, int month) {
		boolean run = false;
		int days = 0;
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			days = 31;
		} else if (month == 4 || month == 6 || month == 9 || month == 11) {
			days = 30;
		} else if (month == 2) {
			run = isLeap(year);
			if (run == true) {// 闰年29天
				days = 29;
			} else {
				days = 28;
			}
		}
		return days;
	}

	// 距离1900年1月1日的总天数
	public static int totalDays(int year, int month) {

		int totalDays = 0;
		for (int i = 1900; i < year; i++) {
			if (isLeap(i) == true) {
				totalDays += 366;
			} else {
				totalDays += 365;
			}
		}
		for (int j = 1; j <= month - 1; j++) {
			switch (j) {
			case 1:
				totalDays += 31;
				break;
			case 2:
				if (isLeap(year)) {
					totalDays += 29;
				} else {
					totalDays += 28;
				}
				break;
			case 3:
				totalDays += 31;
				break;
			case 4:
				totalDays += 30;
				break;
			case 5:
				totalDays += 31;
				break;
			case 6:
				totalDays += 30;
				break;
			case 7:
				totalDays += 31;
				break;
			case 8:
				totalDays += 31;
				break;
			case 9:
				totalDays += 30;
				break;
			case 10:
				totalDays += 31;
				break;
			case 11:
				totalDays += 30;
				break;
			case 12:
				totalDays += 31;
				break;
			default:
				break;
			}
		}
		return totalDays;
	}

	public static void printCalender(int year, int month) {
		int week = 0;
		System.out.println("\t\t\t\t   " + year + " 年      " + month + " 月 ");
		System.out
				.println("\n\t\t--------------------------------------------------"
						+ "\n\t\t日\t一\t二\t三\t四\t五\t六");
		week = totalDays(year, month);// 判断周几
		week = (week % 7) + 1;
		// 打印空格
		System.out.print("\t\t");
		for (int Null = 0; Null < week; Null++) {
			if (week == 7) {
				break;
			}
			System.out.print("  \t");
		}

		int kday = days(year, month);
		for (int k = 1; k <= kday; k++) {
			System.out.printf("%2d\t", k);
			week++;
			if (week % 7 == 0) {
				System.out.println();
				System.out.print("\t\t");
			}
		}
	}
}
